package org.qing.slice.excel.utils;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.qing.slice.excel.utils.ExcelOperationUtil.StyleMapping;

import javafx.application.Platform;

@SuppressWarnings("restriction")
public class SliceUtil {

	public static String slice() throws Exception {
		String fileName = StatusUtil.src.getName().substring(0, StatusUtil.src.getName().lastIndexOf("."));
		String suffx = StatusUtil.src.getName().substring(StatusUtil.src.getName().lastIndexOf("."),
				StatusUtil.src.getName().length());
		// 分片目录
		File slice = new File(StatusUtil.src.getParentFile().getAbsolutePath() + File.separatorChar + fileName + suffx + "_分段文件夹");
		// 拷贝一个新文件来处理
		File des = new File(slice.getAbsolutePath() + File.separatorChar + fileName + "_tmp" + suffx);
		if (suffx.startsWith(".xls")) {
			FileUtils.copy(StatusUtil.src, des);
			Workbook srcWorkbook = ExcelUtils.getWorkbook(des.getAbsolutePath());
			Sheet srcSheet = srcWorkbook.getSheetAt(0);
			Row title = srcSheet.getRow(0);
			int currentFile = 1;
			Workbook sliceTmpWorkbook = null;
			StyleMapping mapping = null;
			File sliceTmp = null;
			FileOutputStream sliceTmpFileOutputStream = null;
			Sheet sliceTmpSheet = null;
			int currentlimit = StatusUtil.limit;
			int rowNos = srcSheet.getLastRowNum();
			for (int currentIndex = 1; currentIndex < rowNos; currentIndex++) {
				if (currentlimit == StatusUtil.limit) {
					System.out.println("正在处理标题头");
					// 写文件头部
					sliceTmp = new File(
							slice.getAbsolutePath() + File.separatorChar + fileName + "_slice_" + currentFile + suffx);
					sliceTmpFileOutputStream = new FileOutputStream(sliceTmp);
					if (".xls".equals(suffx.toLowerCase())) {
						sliceTmpWorkbook = new HSSFWorkbook();
					} else {
						sliceTmpWorkbook = new XSSFWorkbook();
					}
					sliceTmpSheet = sliceTmpWorkbook.createSheet();
					mapping = ExcelOperationUtil.copyCellStyle(srcWorkbook, sliceTmpWorkbook);
					ExcelOperationUtil.copyRow(title, sliceTmpSheet.createRow(0),mapping);
					System.out.println("已处理标题头");
				}
				System.out.println("正在处理第"+currentIndex+"行数据");
				Row srcRow = srcSheet.getRow(currentIndex);
				ExcelOperationUtil.copyRow(srcRow, sliceTmpSheet.createRow(StatusUtil.limit - (currentlimit - 1)),mapping);
				System.out.println("已处理处理第"+currentIndex+"行数据");
				currentlimit--;
				if (currentlimit == 0 || !(currentIndex < rowNos - 1)) {
					sliceTmpWorkbook.write(sliceTmpFileOutputStream);
					sliceTmpFileOutputStream.flush();
					sliceTmpFileOutputStream.close();
					sliceTmpWorkbook.close();
					currentlimit = StatusUtil.limit;
					currentFile++;
				}
				double progressD = (double) currentIndex / (double) rowNos;
				System.out.println("处理进度："+(int)(progressD*100) + "%");
				Platform.runLater(() -> StatusUtil.progressFileBar.setProgress(progressD));
			}
			srcWorkbook.close();
			StatusUtil.progressFileBar.setProgress(1.00);
			System.out.println("处理完成。");
			des.delete();
		}
		return new File(
				slice.getAbsolutePath() + File.separatorChar + fileName + "_slice_" + 1 + suffx).getAbsolutePath();
	}
	
	 public static void openFileLocation(String path) {
		 try {
             Runtime.getRuntime().exec("explorer.exe /select," + path);
         } catch (Exception e) {
        	 e.printStackTrace();
         }
     }
}
